package com.qingfeng.client.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qingfeng.base.model.Page;
import com.qingfeng.client.VO.AfterSellQueryVo;
import com.qingfeng.client.bean.Seller;
import com.qingfeng.client.dao.AfterSellMapper;
import com.qingfeng.client.service.AfterSellService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "售后管理")
@Slf4j
@RestController
@RequestMapping("/api/afterSell")
public class AfterSellAPI extends BaseV1Api {

    @Resource
    private AfterSellMapper afterSellDao;

    @Autowired
    private AfterSellService afterSellService;


    @PostMapping(value = "/list")
    public Object list(@RequestBody JSONObject jsonObject) {
        Seller sellerBean = getUser();
        Page page = parsePageJSON(jsonObject.getJSONObject("page"));
        AfterSellQueryVo aftersellBean  = JSON.toJavaObject(jsonObject.getJSONObject("item"), AfterSellQueryVo.class);
        IPage page1 = afterSellDao.getPageList(page, aftersellBean, sellerBean.getStoreId());
        return successPage(page1);
    }


    /**
     * 同意退款
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/agree/{code}")
    public Object agree(@PathVariable(value = "code") String code) {
        String msg = afterSellService.agreeRefund(code);
        if (StringUtils.isEmpty(msg)) {
            return success(null);
        } else {
            return error(msg);
        }
    }

    /**
     * 不同意退款
     * @param code 退款编码
     * @return
     */
    @RequestMapping(value = "/disagree1/{code}")
    public Object disagree(@PathVariable(value = "code") String code) {
        String msg = afterSellService.disagree(code);
        if (StringUtils.isEmpty(msg)) {
            return success(null);
        } else {
            return error(msg);
        }
    }

}
